package com.liangzai.spring.demo.controller;

import com.liangzai.spring.demo.model.UserInfo;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/request")
public class RequestController {

    @RequestMapping("/r1")
    public String r1(String name){
        return "参数值：name="+name;
    }

    @RequestMapping("/r2")
    public String r2(UserInfo user){
        return "参数值：user="+user.toString();
    }

    @RequestMapping("/r3")
    public String r3(Integer numbers){
        return "参数值："+numbers;
    }

    @RequestMapping("/r4")
    public String r4(int numbers){
        return "参数值：numbers="+numbers;
    }

    @RequestMapping("/r5")
    public String r5(@RequestParam("p") String key){
        return "参数值："+key;
    }

    @RequestMapping("/r6")
    public String r6(int[][] arr){
        return "参数值：arr="+ Arrays.toString(arr);
    }

    @RequestMapping("/r7")
    public String r7(@RequestBody UserInfo userInfo){
        return "用户信息:"+userInfo.toString();
    }

    @RequestMapping("/r8")
    public String r8(String[] arr){
        return "返回数据:"+Arrays.toString(arr);
    }

    @RequestMapping("/r9")
    public String r9(@RequestParam List<String> list){
        return "返回数据:"+list;
    }

    @RequestMapping("/r10")
    public String r10(@RequestBody UserInfo userInfo){
        return userInfo.toString();
    }

    @RequestMapping("/r11/{type}/{articleId}")
    public String r11(@PathVariable String type,@PathVariable Integer articleId){
        return "文章类型:"+type+"文章Id"+articleId;
    }

    @RequestMapping("r12")
    public String r12(MultipartFile file) throws IOException {
        String name = file.getOriginalFilename();
        file.transferTo(new File("D:\\ProgramData"+file.getOriginalFilename()));
        return name+"文件上传成功!";
    }

    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request){
        //创建cookie对象
        Cookie[] cookies = request.getCookies();
        //遍历cookie
        List<String> list = new ArrayList<>();
        for (Cookie cookie : cookies){
            list.add(cookie.getName()+" : "+cookie.getValue());
        }
        return list.toString()+"获取cookie成功!";
    }

    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("name") String name){
        return "从cookie中获取的name："+name;
    }


    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        //创建一个空的session对象
        HttpSession session = request.getSession();
        //给session赋值
        session.setAttribute("name","liangzai");
        session.setAttribute("age",18);
        return "session设置成功!";
    }

    @RequestMapping("/getSession")
    public String getSession (HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(session==null){
            return "用户未登录!";
        }else {
            return session.getAttribute("name")+"已登陆";
        }
    }

    @RequestMapping("/getHost")
    public String getHost(HttpServletRequest request){
        String host = request.getRemoteHost();
        return host;
    }

    @RequestMapping("/getHost2")
    public String getHost2(@RequestHeader("Host") String host){
        return "从header中获得host"+host;
    }
}
